﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Web;
using System.Net;
using System.IO.Compression;
using System.IO;
using System.Threading;
using System.Runtime.InteropServices;

namespace BaccaratRally.Client
{
    public partial class FormLogin : Form
    {
        public FormLogin()
        {
            InitializeComponent();
            //清除IE临时文件
            ShellExecute(IntPtr.Zero, "open", "rundll32.exe", " InetCpl.cpl,ClearMyTracksByProcess 8", "", ShowCommands.SW_HIDE);

            this.AcceptButton = btnLogin;
            if (ConfigurationManager.AppSettings["AccountName"] != "" && ConfigurationManager.AppSettings["Password"] != "")
            {
                this.txbUserName.Text = ConfigurationManager.AppSettings["AccountName"];
                this.txbPassword.Text = ConfigurationManager.AppSettings["Password"];
            }
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            Login(this.txbUserName.Text, this.txbPassword.Text);
        }

        private void Login(String accountName, string password)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(ConfigurationManager.AppSettings["ServiceUrl"] + "Login.aspx?UserName=" + accountName + "&Password=" + password);

            #region Build HttpRequest
            request.Method = "GET";
            request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate,sdch");
            request.Accept = "application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
            #endregion

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            String resultstring = "";

            if (response.ContentEncoding == "gzip")
            {
                Stream outStream = new GZipStream(response.GetResponseStream(), CompressionMode.Decompress, true);
                StreamReader reader = new StreamReader(outStream, Encoding.GetEncoding("UTF-8"));
                resultstring = reader.ReadToEnd();
                reader.Close();
                outStream.Close();
            }
            else
            {
                StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("UTF-8"));
                resultstring = reader.ReadToEnd();
                reader.Close();
            }

            if (resultstring.Contains("|"))
            {
                CurrUser.UserName = resultstring.Split(new char[] { '|' })[0];
                CurrUser.UserSetting = resultstring.Split(new char[] { '|' })[1].Split(new char[] { ',' });

                this.DialogResult = System.Windows.Forms.DialogResult.OK;
            }
            else
            {
                MessageBox.Show("登陆失败!");
            }
        }

        public enum ShowCommands : int
        {
            SW_HIDE = 0,
            SW_SHOWNOrmAL = 1,
            SW_NOrmAL = 1,
            SW_SHOWMINIMIZED = 2,
            SW_SHOWMAXIMIZED = 3,
            SW_MAXIMIZE = 3,
            SW_SHOWNOACTIVATE = 4,
            SW_SHOW = 5,
            SW_MINIMIZE = 6,
            SW_SHOWMINNOACTIVE = 7,
            SW_SHOWNA = 8,
            SW_RESTORE = 9,
            SW_SHOWDEFAULT = 10,
            SW_FORCEMINIMIZE = 11,
            SW_MAX = 11
        }
        [DllImport("shell32.dll")]
        static extern IntPtr ShellExecute(IntPtr hwnd, string lpOperation, string lpFile, string lpParameters, string lpDirectory, ShowCommands nShowCmd);
        

    }
}
